package com.skaura.cryserver.code;

import com.skaura.cryserver.model.MessageWapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.charset.Charset;

public class CustomEncoder extends MessageToByteEncoder<MessageWapper> {
    protected void encode(ChannelHandlerContext channelHandlerContext, MessageWapper messageWapper, ByteBuf byteBuf) throws Exception {
        if (null == messageWapper) {
            throw new Exception("msg is null");
        }
        byteBuf.writeShort(messageWapper.getType());
        byteBuf.writeShort(messageWapper.getLen());
        byteBuf.writeBytes(messageWapper.getBody().getBytes(Charset.forName("utf-8")));
    }
}
